home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_100 / 182_01 / bbscport.c < prev    next >
Text File  |  1990-07-30  |  4KB  |  154 lines

  1. /*
  2.     bbscport.c
  3.  
  4.     Written By Mike Kelly
  5.  
  6.     Modified to run under UNIX I/O control by Dave Stanhope.
  7.  
  8.     03/09/83 v1.0   written
  9.     07/08/83 v1.0   Added code to look for modem carrier detect,
  10.             and exit() if not still connected.
  11. */
  12.  
  13. #include "bbscdef.h"
  14. #include <sgtty.h>
  15. #include <sys/ioctl.h>
  16.  
  17. #define LASTDATE  " 07/08/83 "
  18.  
  19. #define PGMNAME "BBSCPORT "
  20. #define VERSION " 1.0 "
  21.  
  22. portinstat()    /*  returns 1 if no char, 0 if char waiting */
  23.     {
  24.     long count  ;
  25.         ioctl(STDIN, FIONREAD, &count) ; /* see if any previous data  */
  26.     if(count > 0) return(0) ; else return(1) ;
  27.     }
  28.   
  29. char portin()       /* get one byte from the port */
  30.     {
  31.     char byte;
  32.     if(read(STDIN, &byte, 1) != 1) return(CPMEOF) ;
  33.     return(byte & 0x7F);
  34.     }
  35.  
  36. portsin(buf,max)    /* get a line of input max. chars long */
  37. int max ; char *buf ;
  38.     {
  39.     int cnt, byte ; char bytex ;
  40.     cnt = 0;
  41.     byte = FALSE;
  42.     while (++cnt <= max && byte != '\r')
  43.         {
  44.         while((byte = portin()) < ' ' || byte > '}')
  45.             {
  46.             if (byte == '\r') { break ; } /* carriage return */
  47.             if (byte == '\b' && cnt > 1)    /* backspace */
  48.                 {
  49.                 portout(byte);
  50.                 portout(' ');
  51.                 portout(byte);
  52.                 *buf--; /* backout last char */
  53.                 cnt--;  /* decrement count too */
  54.                 }
  55.             }
  56.         if (byte != '\r')
  57.             {
  58.             *buf++ = byte;
  59.             }
  60.         portout(byte);  /* echo good chars only */
  61.         }
  62.     *buf++  = '\0';         /* tag \0 on end */
  63.     }
  64.  
  65. portout(byte)       /* send one byte to the port */
  66. char byte;      /* return CTL_K for those times want to check */
  67.     {       /* if the person wants to stop sending        */
  68.     char byte0 ;
  69.     if (portinstat() == 0)          /* == 0 means char waiting */
  70.         {
  71.         if ((byte0 = portin()) == 0x13) /* ctl-S?, then */
  72.             {           /*  simulate xon/xoff */
  73.             portin();       /* gobble up the char */
  74.             }
  75.         if (byte0 == CTL_K || byte0 == 'K' || byte0 == 'k')
  76.             {           /* look for stop sending key */
  77.             stop_that = TRUE;   /* if so, set switch on      */
  78.             }
  79.         }
  80.     byte0 = byte ; write(STDOUT,&byte0,1) ; /* send the byte */
  81.     return(OK) ;
  82.     }
  83.   
  84. portsout(string)    /* send a string to the port */
  85. char *string ;
  86.     {
  87.     char byte ;
  88.  
  89.     while (byte = (*string++))
  90.         {
  91.         portout(byte) ;     /* send one byte at a time */
  92.         }
  93.     }
  94.  
  95. portlsout(string,len)  /* send a string to the port, pad to length */
  96. char *string ; int len ;
  97.     {
  98.     char byte ;
  99.  
  100.     while (byte = (*string++))
  101.         {
  102.         portout(byte) ;     /* send one byte at a time */
  103.         len-- ;
  104.         }
  105.     while (len > 0) { portout(' ') ; len-- ; } /* pad with spaces */
  106.     }
  107.  
  108. porttype(tbuf)      /* type a file to the port */
  109. FILE    *tbuf ;
  110.     {
  111.     char byte ;
  112.  
  113.     stop_that = FALSE;  /* reset switch */
  114.     portsout("\r\nType ctl-K or K to skip this\r\n\n");
  115.     while (((byte = getc (tbuf)) != EOF) && (byte != CPMEOF))
  116.         {
  117.         if(byte == '\n') portout('\r') ;
  118.         portout(byte);
  119.         if (stop_that)       /* received ctl-K or K */
  120.             {
  121.             portsout(CRLF);
  122.             stop_that = FALSE;  /* reset switch */
  123.             return;         /* nuf's enough */
  124.             }
  125.         }
  126.     }
  127.  
  128. portinit()
  129.     {
  130.     /* set raw mode for this terminal */
  131.     struct sgttyb arg;
  132.     ioctl (STDIN, TIOCGETP, &arg);
  133.     arg.sg_flags |= RAW ;
  134.     arg.sg_flags &= ~ECHO;
  135.     ioctl (STDIN, TIOCSETP, &arg);
  136.     }
  137.  
  138. portrst()
  139.     {
  140.     /* set raw mode for this terminal */
  141.     struct sgttyb arg;
  142.     ioctl (STDIN, TIOCGETP, &arg);
  143.     arg.sg_flags &= ~RAW;
  144.     arg.sg_flags |= ECHO ;
  145.     ioctl (STDIN, TIOCSETP, &arg);
  146.     }
  147.  
  148. char gobble()               /* gobble up any answer */
  149.     {
  150.     int cnt = 0 ;
  151.     while (cnt++ < 20) portin ;
  152.     }
  153.  
  154. /*  end of program      */